// pages/user/register/register.js
const handler = require("../../../utils/function.js");
var api = require("../../../api.js"),
    app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        sendtxt: "获取验证码",
        phone: null,
        phoneTest: false,
        verificationcode: null,
        verificationcodeTest: false,
        sendtxt: '发送验证码',
        gettingCode: true,
        password: null,
        passwordTest: false,
        successCallback: null,
        agreement: {
            stauts: false,
          image: "http://run.4hl.cn/addons/sd_drviing/core/public/WeChat/driver/icon-34.png"
        },
    },
    // phoneinput 获取电话号码
    phoneinput: function(event) {
        var that = this
        that.setData({
            phone: event.detail.value
        })
    },
 
    // 发送验证码
    sendcode: function() {
        var that = this;
        console.log(this.data.phone);
        if (this.data.phone == "" || !/^1\d{10}$/.test(this.data.phone)) {
            wx.showToast({
              title: '手机号格式不正确',
              icon: 'none'
            });
            return false;
        }
        wx.showLoading({})
        app.request({
            url: api.user.send_sms,
            method: 'post',
            data: {
                phone: that.data.phone,
            },
            success: function(rss) {
              wx.hideLoading()
                if (rss.code == 0) {
                    handler.showToast('验证码获取失败', "none")
                    return;
                } else {
                  that.setData({ gettingCode:false});
                  that.data.gettingCode = false;
                    that.countDown();
                }
            }
        })
    },
    //倒计时 倒数
    countDown: function() {
        var that = this;
        
        var seconds = 59;
        var countTimer = setInterval(function() {
            that.setData({
                sendtxt: seconds + "s"
            })
            seconds--;
            if (seconds <= 0) {
                countTimer = clearInterval(countTimer);
                that.setData({
                    sendtxt: '获取验证码'
                })
              that.setData({ gettingCode: true });
                that.data.gettingCode = true;
            }
        }, 1000);
    },
    verificationinput: function(e) {
        this.setData({
            verificationcode: e.detail.value,
        });
        this.verificationTest(e.detail.value);
    },
    verificationTest: function(params) {
        if (params == null) {
            wx.showToast({
                title: '请输入验证码',
                icon: "none",
                duration:5000
            })
            return;
        } else {
            this.setData({
                verificationcodeTest: true,
            });
        }
    },
  
    // 修改手机号
    getPhoneLogin: function() {
        var that = this;
        let phone = this.data.phone, code = this.data.verificationcode;
        console.log(phone, code);
        if (!code) {
            wx.showToast({
              title: '请输入验证码',
              icon: 'none'
            });
            return false;
        }
        app.request({
            url: api.user.editPhone,
            method: 'post',
            data: {
                phone,
                code
            },
            success: function (res) {
                wx.showToast({
                    title: res.msg,
                    icon: 'none'
                  })
                if (res.code == 1) {
                    let user = wx.getStorageSync('user_info');
                    user.phone = phone;
                    wx.setStorageSync('user_info', user);
                    setTimeout(() => {
                        wx.navigateBack();
                    }, 1000)
                }
               
            }
        })
        console.log("修改")
    },
 

  // 用户分享
  onShareAppMessage: function () {
   
  },
})